package com.qf.smsplatform.strategy.service.impl;

import com.qf.smsplatform.common.constants.CacheConstants;
import com.qf.smsplatform.common.constants.StrategyConstants;
import com.qf.smsplatform.common.index.StandardSubmit;
import com.qf.smsplatform.strategy.api.CacheService;
import com.qf.smsplatform.strategy.service.FilterChain;
import com.qf.smsplatform.strategy.util.PushMessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ResourceProperties;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Set;
import java.util.function.DoubleBinaryOperator;


@Service(value = "limit")
@Slf4j
public class LimitStrategy implements FilterChain {

    @Resource
    private CacheService cacheService;

    @Autowired
    PushMessageUtil pushMessageUtil;

    public static void main(String[] args) {
        System.out.println(System.currentTimeMillis()- 60 * 30 * 1000);
    }
    @Override
    public boolean strategy(StandardSubmit submit) {
        log.info("【限流策略】  限流策略执行啦");



        //30s一条短信
        //1.获取当前系统时间
        String key = CacheConstants.CACHE_PREFIX_SMS_LIMIT + submit.getDestMobile();

        double timeMills = System.currentTimeMillis();


        //2.获取30s前的系统时间
        double thirtySecond = timeMills - 3 * 1000;


        //3.调用cache模块的rangebyscore命令，查询结果即可
       Set<Object> set = cacheService.zRangeByscore(key,thirtySecond,timeMills);

        //4.结果大于等于1，限流未通过
        if (set.size()>=1){
            log.info("【限流策略】  限流策略未通过，30s内，发过一条");
            pushMessageUtil.pushLog(submit, StrategyConstants.STRATEGY_ERROR_LIMIT);
            pushMessageUtil.pushReport(submit,StrategyConstants.STRATEGY_ERROR_LIMIT);
            return  false;
        }




        //一个小时三条短信
        //1.获取当前系统时间



        //2.获取一小时前的系统时间
        double oneHour = timeMills - 60 * 60 * 1000;

        //3.调用cache模块的rangebyscore命令，查询结果即可
       set = cacheService.zRangeByscore(key, oneHour, thirtySecond);



        //4.结果大于等于3，，限流未通过
        if (set.size()>=3){
            log.info("【限流策略】  限流策略未通过，一个小时内，发送过三条");
            pushMessageUtil.pushLog(submit,StrategyConstants.STRATEGY_ERROR_LIMIT);
            pushMessageUtil.pushReport(submit,StrategyConstants.STRATEGY_ERROR_LIMIT);
            return  false;
        }


        //一天十条短信
        //1.获取当前系统时间


        //2.获取一小时前的系统时间
        double oneDay = timeMills - 60 * 60 * 24 * 1000;

        //3.调用cache模块的rangebyscore命令，查询结果即可
        set = cacheService.zRangeByscore(key, oneDay, thirtySecond);

        //4.结果大于等于10，限流未通过

        if (set.size()>=10){
            log.info("【限流策略】  限流策略未通过，一填内，发送过十条");
            pushMessageUtil.pushLog(submit,StrategyConstants.STRATEGY_ERROR_LIMIT);
            pushMessageUtil.pushReport(submit,StrategyConstants.STRATEGY_ERROR_LIMIT);
            return  false;
        }
        log.info("【限流策略】 限流策略通过!!");
        return true;
    }
}
